define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionorderproduct/index' + location.search,
                    add_url: 'feditionorderproduct/add',
                    edit_url: 'feditionorderproduct/edit',
                    del_url: 'feditionorderproduct/del',
                    multi_url: 'feditionorderproduct/multi',
                    import_url: 'feditionorderproduct/import',
                    table: 'fedition_orderproduct',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        //{field: 'id', title: __('Id')},

                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        //{field: 'product_id', title: __('Product_id'), operate: 'LIKE'},
                        //{field: 'accounturl', title: __('Accounturl'), formatter: Table.api.formatter.url},
                        //{field: 'isproduct', title: __('Isproduct')},
                        //{field: 'producttime', title: __('Producttime'), operate: 'LIKE'},
                        //{field: 'errors', title: __('Errors'), operate: 'LIKE'},
                        {field: 'product_name', title: __('商品名称'), operate: 'LIKE'},
                        {field: 'handle_url', title: __('商品地址'), operate: false},
                        //{field: 'customers_time', title: __('Customers_time'), operate: 'LIKE'},
                        //{field: 'customers_status', title: __('Customers_status'), operate: 'LIKE', formatter: Table.api.formatter.status},
                        //{field: 'customers_id', title: __('Customers_id'), operate: 'LIKE'},
                        //{field: 'createtime', title: __('订单开始时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'user_id', title: __('商品归属人'), formatter:Table.api.formatter.flag, operate: 'LIKE'},
                        // {field: 'zorder', title: __('订单数量'), operate: false},
                        {field: 'zproduct', title: __('卖出商品数量'), operate: false},
                        // {field: 'zprice', title: __('总金额'), operate: false},
                        {field: 'new_material', title: __('商品成本'), operate: false},
                        {field: 'order_id', title: __('某个订单号'), operate: false},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('更新商品'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('更新商品最新数据'),
                                    title: __('更新商品最新数据'),
                                    classname: 'btn btn-sm btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionordershopify/updateordertime?ids_p={order_id}',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#zorder").text(data.Statistics.zorder);
                $("#zproduct").text(data.Statistics.zproduct);
                $("#zprice").text(data.Statistics.new_material);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});